package com.baima46.core;

import cn.hutool.core.util.ReflectUtil;
import cn.hutool.core.util.StrUtil;
import com.baima46.annotion.ExcelHeader;
import com.baima46.annotion.ExcelHeaderCol;
import com.baima46.annotion.ExcelParent;
import com.baima46.model.ExcelInfo;
import com.baima46.model.SheetInfo;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;

/**
 * @Description 数据处理类
 * @Author yuan cong
 * @Date 2020/12/4 0004
 */
public class DataHandle {

    /**
     * 解析数据内容集合
     * @param sheetInfo
     * @return
     */
    public static List<List<Object>> getData(SheetInfo sheetInfo) throws NoSuchFieldException {
        //解析数据内容集合
        List<List<Object>> rowList = new ArrayList<>();

        if (sheetInfo.getDataList()==null){
            return rowList;
        }

        for (Object t : sheetInfo.getDataList()) {
            List<Object> valueList = new ArrayList<>();

            //固定表头的值
            List<Field> fieldList = getHeaderFields(t, sheetInfo.getGroup());
            for (Field field : fieldList) {
                Object fieldValue = ReflectUtil.getFieldValue(t, field);
                valueList.add(fieldValue);
            }

            //动态表头的值
            List<Field> DyFieldList = getDyHeaderFields(t.getClass());
            for (Field field : DyFieldList) {
                //动态表头list
                List<Object> fieldValue = (List<Object>) ReflectUtil.getFieldValue(t, field);
                if (fieldValue==null){
                    continue;
                }
                for (Object o : fieldValue) {
                    //单个List里的动态表头的属性
                    List<Field> headerFields = getHeaderFields(o, sheetInfo.getGroup());

                    //获取属性的值
                    for (Field headerField : headerFields) {
                        Object fieldValue1 = ReflectUtil.getFieldValue(o, headerField);
                        valueList.add(fieldValue1);
                    }
                }
            }

            rowList.add(valueList);
        }

        //保存信息
        sheetInfo.setRowList(rowList);

        return rowList;
    }




    private static List<Field> getDyHeaderFields(Class<?> clazz) {
        Field[] declaredFields = clazz.getDeclaredFields();

        //数据类的父类属性
        Class<?> superclass = clazz.getSuperclass();
        Field[] superclassDeclaredFields = superclass.getDeclaredFields();
        //继承父类属性的位置
        ExcelParent excelParent = clazz.getAnnotation(ExcelParent.class);
        int position = 1;
        if (excelParent!=null){
            position = excelParent.value();
        }

        List<Field> fieldNames = new ArrayList<>();

        //如果需要父类属性加在前面
        if (position==1){
            addField(fieldNames, superclassDeclaredFields);
        }

        //添加属性
        addField(fieldNames, declaredFields);

        //如果需要父类属性加在后面
        if (position==2){
            addField(fieldNames, superclassDeclaredFields);
        }

        return fieldNames;
    }

    private static void addField(List<Field> fieldNames, Field[] declaredFields){
        for (int i = 0; i < declaredFields.length; i++) {
            ExcelHeaderCol annotation = declaredFields[i].getAnnotation(ExcelHeaderCol.class);
            if(annotation!=null && "java.util.List".equals(declaredFields[i].getType().getName())){
                fieldNames.add(declaredFields[i]);
            }
        }
    }



    private static List<Field> getHeaderFields(Object t,List<String> groupList) throws NoSuchFieldException {
        Field[] declaredFields = t.getClass().getDeclaredFields();

        //数据类的父类属性
        Class<?> superclass = t.getClass().getSuperclass();
        Field[] superclassDeclaredFields = superclass.getDeclaredFields();
        //继承父类属性的位置
        ExcelParent excelParent = t.getClass().getAnnotation(ExcelParent.class);
        int position = 1;
        if (excelParent!=null){
            position = excelParent.value();
        }

        //属性
        List<Field> fieldNames = new ArrayList<>();

        //如果需要父类属性加在前面
        if (position==1){
            addField(fieldNames, superclassDeclaredFields, t, groupList);
        }

        //添加属性
        addField(fieldNames, declaredFields, t, groupList);

        //如果需要父类属性加在后面
        if (position==2){
            addField(fieldNames, superclassDeclaredFields, t, groupList);
        }

        return fieldNames;
    }

    private static void addField(List<Field> fieldNames, Field[] declaredFields, Object t, List<String> groupList) throws NoSuchFieldException {
        for (int i = 0; i < declaredFields.length; i++) {
            ExcelHeader annotation = declaredFields[i].getAnnotation(ExcelHeader.class);
            if(annotation!=null){
                //组
                String group1 = annotation.group();
                if (!groupList.isEmpty()){
                    //传入的组不为空，则匹配组不同的话，则不取标题
                    if (!groupList.contains(group1)){
                        continue;
                    }
                }

                //如果需要值替换
                String valueReplace = annotation.valueReplace();
                if (StrUtil.isNotEmpty(valueReplace)){
                    Field field = t.getClass().getDeclaredField(valueReplace);
                    fieldNames.add(field);
                    continue;
                }

                fieldNames.add(declaredFields[i]);
            }
        }
    }


}
